package cn.edu.ctbu.eol.teacher.mapper;

import cn.edu.ctbu.eol.teacher.pojo.Teacher;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface TeacherMapper {

    @Select("select * from teachers where employee_number = #{employeeNumber}")
    Teacher getByEmployeeNumber(String employeeNumber);

    @Select("<script>" +
            "SELECT * FROM teachers " +
            "<where>" +
            "<if test='name != null and name != \"\"'>" +
            "AND name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "<if test='employeeNumber != null and employeeNumber != \"\"'>" +
            "AND employee_number = #{employeeNumber}" +
            "</if>" +
            "</where>" +
            "ORDER BY id DESC" +
            " LIMIT #{offset}, #{limit}" +
            "</script>")
    List<Teacher> selectByPage(@Param("name") String name, @Param("employeeNumber") String employeeNumber, @Param("offset") Integer offset, @Param("limit") Integer limit);

    @Select("<script>" +
            "SELECT COUNT(*) FROM teachers " +
            "<where>" +
            "<if test='name != null and name != \"\"'>" +
            "AND name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "<if test='employeeNumber != null and employeeNumber != \"\"'>" +
            "AND employee_number = #{employeeNumber}" +
            "</if>" +
            "</where>" +
            "</script>")
    Integer selectCount(@Param("name") String name, @Param("employeeNumber") String employeeNumber);

    @Insert("INSERT INTO teachers(employee_number, name, gender, age, entry_date, modify_date, password) " +
            "VALUES(#{employeeNumber}, #{name}, #{gender}, #{age}, #{entryDate}, NOW(), #{password})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Teacher teacher);

    @Update("<script>" +
            "UPDATE teachers" +
            "<set>" +
            "  <if test='employeeNumber != null'>employee_number = #{employeeNumber},</if>" +
            "  <if test='name != null'>name = #{name},</if>" +
            "  <if test='gender != null'>gender = #{gender},</if>" +
            "  <if test='age != null'>age = #{age},</if>" +
            "  <if test='entryDate != null'>entry_date = #{entryDate},</if>" +
            "  <if test='password != null'>password = #{password},</if>" +
            "  modify_date = NOW()" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int update(Teacher teacher);

    @Delete("DELETE FROM teachers WHERE id=#{id}")
    int delete(@Param("id") Integer id);
}